package com.lihao.demo1;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.lihao.demo1.databinding.ActivityAddressInfoBinding;
import com.lihao.demo1.utils.KeyboardUtils;
import com.lihao.demo1.utils.SharePreferenceUtil;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class AddressInfoActivity extends AppCompatActivity {

    private static final String NAME ="name" ;
    private static final String PHONE ="phone" ;
    private static final String REGION ="region" ;
    private static final String DETAIL ="detail" ;
    private ActivityAddressInfoBinding binding;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context=this;
        binding = DataBindingUtil.setContentView(this, R.layout.activity_address_info);
        initView();
        initListener();
    }

    private void initListener() {
        binding.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        binding.niceSpinner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                KeyboardUtils.hideSoftInput(AddressInfoActivity.this);
            }
        });
        binding.tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               if(!TextUtils.isEmpty(binding.etName.getText().toString().trim())){
                   SharePreferenceUtil.getInstance(context).setStringValue(NAME, binding.etName.getText().toString().trim());
               }else{
                   Toast.makeText(AddressInfoActivity.this, "收货人不能为空" , Toast.LENGTH_SHORT).show();
                   return;
               }
               if(!TextUtils.isEmpty(binding.etPhone.getText().toString().trim())){
                   SharePreferenceUtil.getInstance(context).setStringValue(PHONE, binding.etPhone.getText().toString().trim());
               }else{
                   Toast.makeText(AddressInfoActivity.this, "手机号码不能为空" , Toast.LENGTH_SHORT).show();
                   return;
               }
               if(!TextUtils.isEmpty(binding.niceSpinner.getText().toString().trim())){
                   SharePreferenceUtil.getInstance(context).setStringValue(REGION, binding.niceSpinner.getText().toString().trim());
               }else{
                   Toast.makeText(AddressInfoActivity.this, "所在地区不能为空" , Toast.LENGTH_SHORT).show();
                   return;
               }
               if(!TextUtils.isEmpty(binding.etDetail.getText().toString().trim())){
                   SharePreferenceUtil.getInstance(context).setStringValue(DETAIL, binding.etDetail.getText().toString().trim());
               }else{
                   Toast.makeText(AddressInfoActivity.this, "您的详细地址不能为空" , Toast.LENGTH_SHORT).show();
                   return;
               }
               StringBuilder stringBuilder=new StringBuilder();
               stringBuilder.append(binding.etName.getText().toString().trim())
                            .append(binding.etPhone.getText().toString().trim())
                            .append(binding.niceSpinner.getText().toString().trim())
                            .append(binding.etDetail.getText().toString().trim());
                Intent intent = getIntent();
                intent.putExtra("address", stringBuilder.toString());
                setResult(111, intent);
                finish();
            }
        });
    }

    private void initView() {
        List<String> data = new LinkedList<>(
                Arrays.asList("北京","上海","广州","深圳","天津","成都","杭州","武汉","重庆","南京","苏州","西安","长沙","沈阳",
                        "青岛","郑州","大连","东莞","宁波","济南","厦门","哈尔滨","长春","福州","石家庄","佛山","烟台","合肥","昆明",
                        "唐山","乌鲁木齐"," 贵阳"," 海口","兰州","银川","西宁","呼和浩特","泉州","包头","南通","大庆","徐州","潍坊",
                        "常州","鄂尔多斯","南昌","绍兴","济宁","盐城"));
        binding.niceSpinner.attachDataSource(data);
        if(!TextUtils.isEmpty(SharePreferenceUtil.getInstance(context).getStringValue(NAME))){
            binding.etName.setText(SharePreferenceUtil.getInstance(context).getStringValue(NAME));
            binding.etPhone.setText(SharePreferenceUtil.getInstance(context).getStringValue(PHONE));
            binding.niceSpinner.setText(SharePreferenceUtil.getInstance(context).getStringValue(REGION));
            binding.etDetail.setText(SharePreferenceUtil.getInstance(context).getStringValue(DETAIL));
        }
    }
}
